using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.AddressableAssets.ResourceLocators;

public class TestConsumer : MonoBehaviour
{
    public string host = "http://localhost";
    public string path = "AADemo";

    public string catalogAxe = "";
    public string keyAxe = "";

    public string catalogFoodPack = "";
    public string keyFoodPack = "";

    public Button buttonAxe;
    public Button buttonFoodPack;

    private void Start()
    {
        //Debug.Log(Addressables.BuildPath);
        //Debug.Log(Addressables.RuntimePath);
        //Debug.Log(Addressables.PlayerBuildDataPath);

        buttonAxe.onClick.AddListener(LoadAxe);
        buttonFoodPack.onClick.AddListener(LoadFoodPack);
    }

    public void LoadAxe()
    {
        string full;

        full = string.Format("{0}/{1}/{2}", host, path, catalogAxe);
        Debug.Log(full);

        Addressables.LoadContentCatalogAsync(full).Completed += LoadContentCatalogAxeCompleted;
    }

    public void LoadFoodPack()
    {
        string full;

        full = string.Format("{0}/{1}/{2}", host, path, catalogFoodPack);
        Debug.Log(full);

        Addressables.LoadContentCatalogAsync(full).Completed += LoadContentCatalogFoodPackCompleted;
    }

    private void LoadContentCatalogAxeCompleted(AsyncOperationHandle<IResourceLocator> obj)
    {
        Debug.Log("LoadContentCatalogAxeCompleted");

        Addressables.InstantiateAsync(keyAxe);
    }

    private void LoadContentCatalogFoodPackCompleted(AsyncOperationHandle<IResourceLocator> obj)
    {
        Debug.Log("LoadContentCatalogFoodPackCompleted");

        Addressables.InstantiateAsync(keyFoodPack, new Vector3(1, 0, 0), Quaternion.identity);
    }
}

